<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" @reset="resetSearch">
      <el-col :span="8" :xs="24" v-show="$permiss[416]">
        <el-form-item :label="$t('所属租户')" prop="adminUserId">
          <el-select style="width: 100%" v-model="page.adminUserId" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('配置项')" prop="rulesKey">
          <el-input v-model="page.rulesKey" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('关键词查询')" prop="describe" label-width="100px">
          <el-input v-model="page.describe" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('状态')" prop="valid">
          <el-select style="width: 100%" v-model="page.valid" clearable>
            <el-option :value="1" :label="$t('启用')"></el-option>
            <el-option :value="0" :label="$t('禁用')"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('规则类型')" prop="type">
          <el-select style="width: 100%" v-model="page.type">
            <el-option :value="0" :label="$t('新客')"></el-option>
            <el-option :value="1" :label="$t('老客')"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </SearchForm>

    <el-card shadow="never" style="margin-top: 10px">
      <Table :page="page" :table-data="tableData" :table-column="tableColumn" @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <el-table-column :label="$t('状态')" align="center">
          <template slot-scope="{row}">
            <el-switch :disabled="!$permiss[393]" @change="changeStatus(row)"
                       :value="row.valid === 1"></el-switch>
          </template>
        </el-table-column>
      </Table>
    </el-card>
  </div>
</template>

<script>
import {selectRules, rulesDisable} from "@/api/product";
import {selectTenant} from "@/api/system";

export default {
  name: 'machineRules',
  data() {
    return {
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
        type: 0
      },
      tableData: [],
      tableColumn: [
        {label: this.$t('配置项'), prop: 'rulesKey', align: 'center', minWidth: 100},
        {label: this.$t('规则描述'), prop: 'rulesDescribe', align: 'center', minWidth: 200},
        {label: this.$t('规则参考值'), prop: 'rulesValue', align: 'center', minWidth: 100},
        {label: this.$t('更新时间'), prop: 'updateTime', align: 'center', minWidth: 150},
      ],
      tenantList: []
    }
  },
  async mounted() {
    this.tenantList = await selectTenant().then(res => res.data)
    this.tenantList.length > 0 ? this.page.adminUserId = this.tenantList[0].adminUserId : null
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const res = await selectRules(this.page)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list.map(item => {
          if (item.rulesValueVice && item.rulesValue) {
            item.rulesValue = item.rulesValue + ' - ' + item.rulesValueVice
          }
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    resetSearch() {
      this.page = {
        pageSize: 10,
        pageNum: 1,
        total: 0,
        type: 0
      }
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    changeStatus(row) {
      const submit = async () => {
        const res = await rulesDisable({
          rulesKey: row.rulesKey,
          valid: row.valid === 1 ? 0 : 1,
          type: this.page.type
        })
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: this.$t('修改成功')
          });
          this.getTableData()
        }
      }
      if (row.valid === 1) {
        this.$confirm(this.$t('当前规则有关联的APP,禁用后可能存在业务风险,请慎重操作!'), this.$t('提示'), {
          type: 'warning'
        }).then(() => {
          submit()
        }).catch(() => {
        });
      } else {
        submit()
      }

    }
  }
}
</script>

<style scoped>

</style>
